package com.example.weather;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;

public class HttpTest {
    public static void main(String[] args) {
        //HttpClient可以理解为一个浏览器,但是又和浏览器不同
        HttpClient httpClient = new HttpClient();
        //详解可以去访问:https://blog.csdn.net/zhangzeyuaaa/article/details/49073727
        httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        try {
            getMessage(httpClient);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void getMessage(HttpClient httpClient) throws Exception {
        //要访问的接口
        String dataUrl="https://restapi.amap.com/v3/weather/weatherInfo?city=430602&key=5624d73206735edac5b871c738adb405";
        //使用get请求获取数据
        GetMethod getMethod = new GetMethod(dataUrl);
        //执行
        httpClient.executeMethod(getMethod);
        //将响应体获取为字符串
        String responseBody = getMethod.getResponseBodyAsString();
        //解析
        JSONObject jsonObject = JSONObject.parseObject(responseBody);
        System.out.println(jsonObject.toJSONString());
    }
}
